package com.linewell.dao;

import com.linewell.entity.Collected;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Title: ssm2
 * @description:
 * @author: hyuhuang@linewell.com
 * @since:2018/2/27
 */
public interface CollectedMapper {

    @ResultMap("dao.mapper.CollectedMapper.CollectedMap")
    @Select("SELECT c.collection_id,a.article_id,a.title,a.article_date,u.user_id FROM collected c,article a,userinfo u WHERE a.article_id=c.article_id AND a.user_id=u.user_id")
    List<Collected> getAll();

    @Select("SELECT COUNT(userId) FROM collected WHERE articleId = ${articleId}")
    int getCollectedById(@Param("articleId") long articleId);

    @Insert("insert into collected(articleId,userId) values(${article.articleId},${user.userId})")
    int addCollected(Collected c);

    @Delete("delete from collected where collectionId = ${articleId}")
    int deleteCollected(@Param("articleId") long articleId);

    @Delete("delete from collected where articleId = ${articleId} and userId=${userId}")
    int deleteCollectedByUser(@Param("articleId") long articleId,@Param("userId") long userId);

    @ResultMap("dao.mapper.CollectedMapper.CollectedMap")
    @Select("SELECT top 5  c.collection_id,a.article_id,a.title,a.article_date,u.user_id \n" +
            "FROM collected c,article a,userinfo u \n" +
            "WHERE a.article_id=c.article_id AND a.user_id=u.user_id and c.user_id= ${userId} \n" +
            "ORDER BY c.collection_id DESC;")
    List<Collected> selectFive(@Param("userId") long userId);

    @Select("SELECT COUNT(collectionId) FROM collected WHERE articleId = ${articleId} and userId=${userId}")
    int IsCollected(@Param("articleId") long articleId,@Param("userId") long userId);
}
